/*
 * EnigmaGM - A feature rich guild management system.
 * Copyright (C) 2007 ajiaojr.
 * 
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
 * 02110-1301, USA.
 */
package org.ajiaojr.enigmagm.db.mysql;

import org.ajiaojr.enigmagm.client.data.Raid;
import org.ajiaojr.enigmagm.client.service.DataAccessException;
import org.ajiaojr.enigmagm.db.ConnectionManager;
import org.ajiaojr.enigmagm.db.RaidDAO;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class MySQLRaidDAOImpl implements RaidDAO {

  public List findByPointSystemID(int pointSystemId, int firstResult,
      int maxResults) throws DataAccessException {
    Connection c = null;
    try {
      c = ConnectionManager.getInstance().getConnection();

      List result = new ArrayList();

      PreparedStatement s =
          c
              .prepareStatement("SELECT raid_id, raid_instance_name, raid_time, comments FROM raid_list WHERE point_system_id = ? ORDER BY raid_time DESC LIMIT ? , ?");
      s.setInt(1, pointSystemId);
      s.setInt(2, firstResult);
      s.setInt(3, maxResults);

      ResultSet rs = s.executeQuery();

      while (rs.next()) {
        result.add(new Raid(rs.getInt(1), rs.getString(2), rs.getTimestamp(3)
            .getTime(), rs.getString(4), pointSystemId));
      }

      rs.close();
      s.close();

      return result;
    } catch (Exception e) {
      throw new DataAccessException(e.getMessage());
    } finally {
      try {
        c.close();
      } catch (SQLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
      }
    }
  }

  public Raid save(Raid raid) throws DataAccessException {
    // TODO Auto-generated method stub
    return null;
  }

}
